package com.quickpass.order.service;


import com.quickpass.order.client.CouponClient;
import com.quickpass.order.client.DishesClient;
import com.quickpass.order.domain.Order;
import com.quickpass.order.domain.OrderDTO;
import com.quickpass.order.domain.ShopCart;
import com.quickpass.order.mapper.OrderMapper;
import com.quickpass.order.utils.IdWorker;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: 弔人
 * @time: 2023/2/6 9:57
 */
@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishesClient dishesClient;

    @Autowired
    private CouponClient couponCLient;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedissonClient redissonClient;


    RLock orderLock = null;
    @PostConstruct
    public void init(){
        orderLock = redissonClient.getLock("order_lock");
    }


    public AjaxResult createOrder(OrderDTO orderDTO) {


        //获取过滤器
//        RBloomFilter<Object> bloomF = redissonClient.getBloomFilter("tableIdList");
//        boolean contains = bloomFilter.contains(orderDTO.getTableid());
//        if (contains){
//            System.out.println("已存在");
//            return new AjaxResult(0, "该桌号已经有人点单");
//        }else {
            orderLock.lock();
            try {

                System.out.println(orderDTO);

                //订单表添加
                Order order = new Order();
                IdWorker idWorker = new IdWorker();
                long nextId = idWorker.nextId();
                order.setId(nextId);
                order.setBusinessid(orderDTO.getBusinessid());
                order.setTableid(orderDTO.getTableid());
                order.setMemberid(orderDTO.getMemberid());
                order.setCouponid(orderDTO.getCouponid());
                Date now = new Date();
                order.setCreatetime(now);
                order.setPaystatus(1);
                order.setPayamount(orderDTO.getTotalamount());
                orderMapper.insertOrder(order);


                //减优惠券
                System.out.println("优惠券id"+orderDTO.getCouponid());
                couponCLient.usen(orderDTO.getCouponid());
                //扣库存
                Long memberid = orderDTO.getMemberid();
                List<ShopCart> shopCartList = (List<ShopCart>) redisTemplate.opsForHash().values("shopCart:"+memberid);
                shopCartList.forEach(dishes->{
                    System.out.println(order.getId());
                    System.out.println(dishes);
                    dishesClient.subtractStock(dishes.getId(), dishes.getNumber());
                });



                //数据量个误判率
//                bloomFilter.tryInit(1000000L,0.02);
                //tableId插入到布隆过滤器中
//                bloomFilter.add(orderDTO.getTableid());



                return new AjaxResult(1, "下单成功");
            } catch (Exception e) {
                e.printStackTrace();
                return new AjaxResult(0,"出现异常");
            } finally {
                orderLock.unlock();
            }
//        }

    }
}
